#include "kdcaudiovideodelegate.h"

#include <QStringList>
#include <KComboBox>
#include <KLocale>
#include <KIntSpinBox>

#include "kdcaudiovideoeditormodel.h"
#include <kdcmovie.h>
#include <kdclib.h>

KDCAudioVideoDelegate::KDCAudioVideoDelegate( QObject *aParent ) : QItemDelegate( aParent )
{

}

QWidget* KDCAudioVideoDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
	KDC::DiscType discType = (KDC::DiscType)index.data( KDCAudioVideoEditorModel::role_DiscType ).toInt();
	if ( index.row() == 0 ) //video
	{
		switch ( index.column() )
		{
			case KDCAudioVideoEditorModel::col_Mode:
			{
				KComboBox *editor = new KComboBox( parent );
				KDC::StreamMode streamMode;
				/*
				streamMode = p_movie->videoStream()->mode();
				QList<KDCVideoStreamMetaData*> metaData;
				if ( p_movie->movieGroup() )
				{
					KDCMovieGroup *group = p_movie->movieGroup();
					for ( i=0; i<group->count(); ++i )
						metaData.append( group->movie( i )->videoStream()->metaData() );
				}
				else
					metaData.append( p_movie->videoStream()->metaData() );
				if ( type == KDC::DVD )
				{
					for ( i=0; i<metaData.size(); ++i )
						copy.append( KDCVideoStreamMetaData::isDVDPALVideoStream( metaData.at( i ) ) || KDCVideoStreamMetaData::isDVDNTSCVideoStream( metaData.at( i ) ) );
				}
				else
					if ( type == KDC::CVD )
					{
						for ( i=0; i<metaData.size(); ++i )
							copy.append( KDCVideoStreamMetaData::isCVDPALVideoStream( metaData.at( i ) ) || KDCVideoStreamMetaData::isCVDNTSCVideoStream( metaData.at( i ) ) );
					}
				else
					if ( type == KDC::SVCD )
					{
						for ( i=0; i<metaData.size(); ++i )
							copy.append( KDCVideoStreamMetaData::isSVCDPALVideoStream( metaData.at( i ) ) || KDCVideoStreamMetaData::isSVCDNTSCVideoStream( metaData.at( i ) ) );
					}
				else
					if ( type == KDC::VCD )
					{
						for ( i=0; i<metaData.size(); ++i )
							copy.append( KDCVideoStreamMetaData::isVCDPALVideoStream( metaData.at( i ) ) || KDCVideoStreamMetaData::isVCDNTSCVideoStream( metaData.at( i ) ) );
					}
				copy.removeAll( TRUE );
				if ( copy.isEmpty() )
					editor->addItem( "Copy", KDC::Copy );
				*/
				editor->addItem( i18n("Resample"), KDC::Resample );
				return editor;
			}
			case KDCAudioVideoEditorModel::col_Codec:
			{
				KComboBox *editor = new KComboBox( parent );
				if ( discType == KDC::DVD )
				{
					editor->addItem( toString( KDC::MPEG1 ), KDC::MPEG1 );
					editor->addItem( toString( KDC::MPEG2 ), KDC::MPEG2 );
				}
				else
					if ( discType == KDC::VCD )
						editor->addItem( toString( KDC::MPEG1 ), KDC::MPEG1 );
				else
					editor->addItem( toString( KDC::MPEG2 ), KDC::MPEG2 );
				return editor;
			}
			case KDCAudioVideoEditorModel::col_System:
			{
				KComboBox *editor = new KComboBox( parent );
				editor->addItem( toString( KDC::PAL ), KDC::PAL );
				editor->addItem( toString( KDC::NTSC ), KDC::NTSC );
				return editor;
			}
			case KDCAudioVideoEditorModel::col_Fps:
			{
				KComboBox *editor = new KComboBox( parent );
				if ( index.model()->data( index.model()->index( 0, KDCAudioVideoEditorModel::col_System ), Qt::EditRole ).toInt() == KDC::PAL )
					editor->addItem( "25.000", 25.0 );
				else
				{
					if ( discType == KDC::DVD || discType == KDC::VCD )
						editor->addItem( "23.976", 23.976 );
					editor->addItem( "29.970", 29.970 );
				}
				return editor;
			}
			case KDCAudioVideoEditorModel::col_Bitrate:
			{
				KIntSpinBox *editor = new KIntSpinBox( parent );
				if ( discType == KDC::DVD )
				{
					if ( index.model()->data( index.model()->index( 0, KDCAudioVideoEditorModel::col_Codec ), Qt::EditRole ).toInt() == KDC::MPEG1 )
						editor->setRange( 500, 1856 );
					else
						editor->setRange( 500, 9800 );
				}
				else
					if ( discType == KDC::SVCD || discType == KDC::CVD )
						editor->setRange( 500, 2600 );
				else
					editor->setRange( 500, 1152 );
				return editor;
			}
			case KDCAudioVideoEditorModel::col_Ratio:
			{
				KComboBox *editor = new KComboBox( parent );
				if ( discType == KDC::DVD )
				{
					editor->addItem( toString( KDC::LetterBox ), KDC::LetterBox );
					editor->addItem( toString( KDC::WideScreen ), KDC::WideScreen );
				}
				else
					editor->addItem( toString( KDC::LetterBox ), KDC::LetterBox );
				return editor;
			}
			case KDCAudioVideoEditorModel::col_Resolution:
			{
				KComboBox *editor = new KComboBox( parent );
				QStringList items;
				if ( discType == KDC::DVD && index.model()->data( index.model()->index( 0, KDCAudioVideoEditorModel::col_Codec ), Qt::EditRole ).toInt() == KDC::MPEG2 )
				{
					if ( index.model()->data( index.model()->index( 0, KDCAudioVideoEditorModel::col_System ), Qt::EditRole ).toInt() == KDC::PAL )
						items << "720x576" << "704x576" << "352x576" << "352x288";
					else
						items << "720x480" << "704x480" << "352x480" << "352x240";
				}
				else
					if ( discType == KDC::DVD && index.model()->data( index.model()->index( 0, KDCAudioVideoEditorModel::col_Codec ), Qt::EditRole ).toInt() == KDC::MPEG1 )
					{
						if ( index.model()->data( index.model()->index( 0, KDCAudioVideoEditorModel::col_System ), Qt::EditRole ).toInt() == KDC::PAL )
							items << "352x288";
						else
							items << "352x240";
					}
				else
					if ( discType == KDC::SVCD )
					{
						if ( index.model()->data( index.model()->index( 0, KDCAudioVideoEditorModel::col_System ), Qt::EditRole ).toInt() == KDC::PAL )
							items << "480x576";
						else
							items << "480x480";
					}
				else
					if ( discType == KDC::CVD )
					{
						if ( index.model()->data( index.model()->index( 0, KDCAudioVideoEditorModel::col_System ), Qt::EditRole ).toInt() == KDC::PAL )
							items << "352x576";
						else
							items << "352x480";
					}
				else
				{
					if ( index.model()->data( index.model()->index( 0, KDCAudioVideoEditorModel::col_System ), Qt::EditRole ).toInt() == KDC::PAL )
						items << "352x288";
					else
						items << "352x240";
				}
				QStringList tmp;
				foreach ( QString str, items )
				{
					tmp = str.split( "x" );
					editor->addItem( str, QPoint( tmp.first().toInt(), tmp.last().toInt() ) );
				}
				return editor;
			}
		}
	}
	else //audio
	{
		switch ( index.column() )
		{
			case KDCAudioVideoEditorModel::col_Mode:
			{
				KComboBox *editor = new KComboBox( parent );
				KDC::StreamMode streamMode;
				/*
				streamMode = p_movie->audioStreamGroup()->stream( p_id )->mode();
				QList<KDCAudioStreamMetaData*> metaData;
				if ( p_movie->movieGroup() )
				{
					KDCMovieGroup *group = p_movie->movieGroup();
					for ( i=0; i<group->count(); ++i )
						metaData.append( group->movie( i )->audioStreamGroup()->stream( p_id )->metaData() );
				}
				else
					metaData.append( p_movie->audioStreamGroup()->stream( p_id )->metaData() );
				if ( type == KDC::DVD )
				{
					for ( i=0; i<metaData.size(); ++i )
						if ( p_movie->videoFormat() == KDC::PAL )
							copy.append( KDCAudioStreamMetaData::isDVDPALAudioStream( metaData.at( i ) ) );
						else
							copy.append( KDCAudioStreamMetaData::isDVDNTSCAudioStream( metaData.at( i ) ) );
				}
				else
					if ( type == KDC::CVD )
					{
						for ( i=0; i<metaData.size(); ++i )
							copy.append( KDCAudioStreamMetaData::isCVDAudioStream( metaData.at( i ) ) );
					}
				else
					if ( type == KDC::SVCD )
					{
						for ( i=0; i<metaData.size(); ++i )
							copy.append( KDCAudioStreamMetaData::isSVCDAudioStream( metaData.at( i ) ) );
					}
				else
					if ( type == KDC::VCD )
					{
						for ( i=0; i<metaData.size(); ++i )
							copy.append( KDCAudioStreamMetaData::isVCDAudioStream( metaData.at( i ) ) );
					}
				copy.removeAll( TRUE );
				if ( copy.isEmpty() )
					editor->addItem( i18n("Copy"), KDC::Copy );
				*/
				editor->addItem( i18n("Resample"), KDC::Resample );
				return editor;
			}
			case KDCAudioVideoEditorModel::col_Codec:
			{
				KComboBox *editor = new KComboBox( parent );
				if ( discType == KDC::DVD )
				{
					if ( index.model()->data( index.model()->index( 0, KDCAudioVideoEditorModel::col_System ), Qt::EditRole ).toInt() == KDC::PAL )
						editor->addItem( toString( KDC::MP2 ), KDC::MP2 );
					editor->addItem( toString( KDC::AC3 ), KDC::AC3 );
				}
				else
					editor->addItem( toString( KDC::MP2 ), KDC::MP2 );
				return editor;
			}
			case KDCAudioVideoEditorModel::col_Channels:
			{
				KComboBox *editor = new KComboBox( parent );
				editor->addItem( "2", 2 );
				/*
				int max;
				if ( p_movie->movieGroup() )
				{
					KDCMovieGroup *group = p_movie->movieGroup();
					max = group->movie( 0 )->audioStreamGroup()->stream( p_id )->metaData()->channelCount();
					for ( int i=1; i<group->count(); ++i )
						max = qMin( max, group->movie( i )->audioStreamGroup()->stream( p_id )->metaData()->channelCount() );
				}
				else
					max = p_movie->audioStreamGroup()->stream( p_id )->metaData()->channelCount();
				if ( p_movie->audioStreamGroup()->stream( p_id )->codec() != KDC::MP2 && max == 6 )
					editor->addItem( "6", 6 );
				*/
				return editor;
			}
			case KDCAudioVideoEditorModel::col_Bitrate:
			{
				KComboBox *editor = new KComboBox( parent );
				if ( discType == KDC::DVD || discType == KDC::SVCD || discType == KDC::CVD )
				{
					editor->addItem( bitrateAsString( 32 ), 32 );
					editor->addItem( bitrateAsString( 48 ), 48 );
					editor->addItem( bitrateAsString( 92 ), 92 );
					editor->addItem( bitrateAsString( 96 ), 96 );
					editor->addItem( bitrateAsString( 128 ), 128 );
					editor->addItem( bitrateAsString( 192 ), 192 );
					editor->addItem( bitrateAsString( 224 ), 224 );
					editor->addItem( bitrateAsString( 384 ), 384 );
				}
				if ( discType == KDC::DVD )
				{
					editor->addItem( bitrateAsString( 448 ), 448 );
					editor->addItem( bitrateAsString( 1536 ), 1536 );
				}
				if ( discType == KDC::VCD )
					editor->addItem( bitrateAsString( 224 ), 224 );
				return editor;
			}
			case KDCAudioVideoEditorModel::col_Delay:
			{
				KIntSpinBox *editor = new KIntSpinBox( parent );
				editor->setRange( -5000, 5000 );
				return editor;
			}
		}
	}
	return NULL;
}

void KDCAudioVideoDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex& index ) const
{
	KDC::DiscType discType = (KDC::DiscType)index.data( KDCAudioVideoEditorModel::role_DiscType ).toInt();
	if ( index.row() == 0 ) //video
	{
		switch ( index.column() )
		{
			case KDCAudioVideoEditorModel::col_Mode:
			case KDCAudioVideoEditorModel::col_Codec:
			case KDCAudioVideoEditorModel::col_System:
			case KDCAudioVideoEditorModel::col_Fps:
			case KDCAudioVideoEditorModel::col_Ratio:
			{
				KComboBox *tmp = qobject_cast<KComboBox*>( editor );
				model->setData( index, tmp->itemData( tmp->currentIndex() ), Qt::EditRole );
				return;
			}
			case KDCAudioVideoEditorModel::col_Bitrate:
			{
				KIntSpinBox *tmp = qobject_cast<KIntSpinBox*>( editor );
				model->setData( index, tmp->value(), Qt::EditRole );
				return;
			}
			case KDCAudioVideoEditorModel::col_Resolution:
			{
				KComboBox *tmp = qobject_cast<KComboBox*>( editor );
				QStringList strList = tmp->itemData( tmp->currentIndex() ).toString().split( "x" );
				QPoint pt( strList.first().toInt(), strList.last().toInt() ); 
				model->setData( index, pt, Qt::EditRole );
				return;
			}
		}
	}
	else //audio
	{
		switch ( index.column() )
		{
			case KDCAudioVideoEditorModel::col_Mode:
			case KDCAudioVideoEditorModel::col_Codec:
			case KDCAudioVideoEditorModel::col_Channels:
			case KDCAudioVideoEditorModel::col_Bitrate:
			{
				KComboBox *tmp = qobject_cast<KComboBox*>( editor );
				model->setData( index, tmp->itemData( tmp->currentIndex() ), Qt::EditRole );
				return;
			}
			case KDCAudioVideoEditorModel::col_Delay:
			{
				KIntSpinBox *tmp = qobject_cast<KIntSpinBox*>( editor );
				model->setData( index, tmp->value(), Qt::EditRole );
				return;
			}
		}
	}
}

void KDCAudioVideoDelegate::setEditorData( QWidget * editor, const QModelIndex & index ) const
{
	KDC::DiscType discType = (KDC::DiscType)index.data( KDCAudioVideoEditorModel::role_DiscType ).toInt();
	if ( index.row() == 0 ) //video
	{
		switch ( index.column() )
		{
			case KDCAudioVideoEditorModel::col_Mode:
			case KDCAudioVideoEditorModel::col_Codec:
			case KDCAudioVideoEditorModel::col_System:
			case KDCAudioVideoEditorModel::col_Fps:
			case KDCAudioVideoEditorModel::col_Ratio:
			case KDCAudioVideoEditorModel::col_Resolution:
			{
				KComboBox *tmp = qobject_cast<KComboBox*>( editor );
				tmp->setCurrentIndex( tmp->findData( index.data( Qt::EditRole ) ) );
				return;
			}
			case KDCAudioVideoEditorModel::col_Bitrate:
			{
				KIntSpinBox *tmp = qobject_cast<KIntSpinBox*>( editor );
				tmp->setValue( index.data( Qt::EditRole ).toInt() );
				return;
			}
		}
	}
	else //audio
	{
		switch ( index.column() )
		{
			case KDCAudioVideoEditorModel::col_Mode:
			case KDCAudioVideoEditorModel::col_Codec:
			case KDCAudioVideoEditorModel::col_Channels:
			case KDCAudioVideoEditorModel::col_Bitrate:
			{
				KComboBox *tmp = qobject_cast<KComboBox*>( editor );
				tmp->setCurrentIndex( tmp->findData( index.data( Qt::EditRole ) ) );
				return;
			}
			case KDCAudioVideoEditorModel::col_Delay:
			{
				KIntSpinBox *tmp = qobject_cast<KIntSpinBox*>( editor );
				tmp->setValue( index.data( Qt::EditRole ).toInt() );
				return;
			}
		}
	}
}
